﻿public class Solution {
    public int NumSquares(int n)
    {
        int[] dp = new int[n + 1];
        Array.Fill(dp, int.MaxValue);
        dp[0] = 0;

        for (int i = 1; i <= Math.Sqrt(n); i++)
        {
            for (int j = i * i; j <= n; j++)
            {
                dp[j] = Math.Min(dp[j], dp[j - i * i] + 1);
            }
        }

        return dp[n];
    }

    public static void Main(string[] args)
    {
        int n = 13;
        Solution solution = new Solution();
        int res = solution.NumSquares(n);
        Console.WriteLine(res);
    }
}